package treeSet;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * TreeSet集合来存储Teacher类型
 */

public class TreeSetDemo3 {
    public static void main(String[] args) {
        TreeSet<Teacher> ts = new TreeSet<>(new Comparator<Teacher>() {
            @Override
            public int compare(Teacher o1, Teacher o2) {
                // o1表示现在要存入的那个元素
                // o2表示已经存入到集合中的元素
                // 主要条件
                int res = o1.getAge() - o2.getAge();
                // 次要条件
                return res != 0 ? res : o1.getName().compareTo(o2.getName());
            }
        });

        Teacher t1 = new Teacher("zhangsn", 23);
        Teacher t2 = new Teacher("lisi", 22);
        Teacher t3 = new Teacher("wangwu", 24);
        Teacher t4 = new Teacher("zhaoliu", 24);
        ts.add(t1);
        ts.add(t2);
        ts.add(t3);
        ts.add(t4);
        System.out.println(ts);
    }
}
